<?php
exit();
/**
 * 一次性任务，已执行完成；
 * 定时把che300车型库同步到本地
 */
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER ['REQUEST_URI'])) {
    die('error');
}
if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}


require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');

while (true) {
    $paimai_db = new Mysql(MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE);
    $sql_01 = 'SELECT `model_id` FROM `au_che300_model`';
    $result_01 = $paimai_db->query($sql_01);
    while ($row_01 = $paimai_db->fetch($result_01)) {
        $rows_01[] = $row_01;
    }
    foreach ($rows_01 as $kr_01 => $vr_01) {
        $che300_model_list_arr[$kr_01] = $vr_01['model_id'];
    }
    $che300_model_list_arr_str = implode(',', $che300_model_list_arr);
    if ($che300_model_list_arr) {
        $condition = "WHERE `model_id` NOT IN (" . $che300_model_list_arr_str . ")";
    }
    $sql_02 = 'SELECT `model_id` FROM `au_car_model` ' . $condition;
    $result_02 = $paimai_db->query($sql_02);
    while ($row_02 = $paimai_db->fetch($result_02)) {
        $rows_02[] = $row_02;
    }
    $i = 0;
    foreach ($rows_02 as $m) {
        $model_res = $paimai_db->query('SELECT * FROM au_car_model WHERE model_id=' . $m['model_id'] . ' limit 1');
        $model = $paimai_db->fetch($model_res);
        if ($model) {
            $info['model'] = $model;
            $series_res = $paimai_db->query('SELECT * FROM au_car_series WHERE series_id=' . $model['series_id'] . ' limit 1');
            $series = $paimai_db->fetch($series_res);
        }
        if ($series) {
            $info['series'] = $series;
            $brand_res = $paimai_db->query('SELECT * FROM au_car_brand WHERE brand_id=' . $series['brand_id'] . ' limit 1');
            $brand = $paimai_db->fetch($brand_res);
        }
        if ($brand) {
            $info['brand'] = $brand;
            $mbrand_res = $paimai_db->query('SELECT * FROM au_car_mbrand WHERE mbrand_id=' . $brand['mbrand_id'] . ' limit 1');
            $mbrand = $paimai_db->fetch($mbrand_res);
        }
        if ($mbrand) {
            $info['mbrand'] = $mbrand;
        }
        $brand = trim($info['brand']['name']);
        $series = trim($info['series']['name']);
        $model_name = trim(str_replace(' ', '', $info['model']['name']));
        $year = intval(trim($info['model']['year']));
        $post_url = "http://api.che300.com/service/identifyModel?token=209e0bb5ef49fe9958fb4a429b99bd1c&brand="
                . $brand . "&series=" . $series . "&model=" . $model_name . "&modelYear=" . $year;
        $res = curlGet($post_url);
        if ($res['status'] === '1') {
            $model_id = $m['model_id'];
            $che300_model_id = $res['modelInfo']['model_id'];
            $total_query = $paimai_db->query('SELECT * FROM au_che300_model WHERE model_id=' . $model_id);
            $num = mysql_num_rows($total_query);
            if ($num == 0) {
                $che300 = $paimai_db->query("INSERT INTO au_che300_model (model_id,che300_model_id) VALUES('{$model_id}','{$che300_model_id}') ");
                echo "数据保存成功" . "<br/>";
                $i++;
                if ($i == 20) {
                    exit();
                }
            } else {
                echo "数据已存在" . "<br/>";
            }
        }
    }

    $paimai_db->close();

    unset($paimai_db);
    $paimai_db = null;
}

function curlGet($url, $decode = true) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $json = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new Exception(curl_errno($ch));
    }
    curl_close($ch);
    return $decode ? json_decode($json, true) : $json;
}
